package com.my.javaweb.list;

import lombok.Data;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 缺陷新增解决
 * @author Felix
 * @date 2023/10/20 20:29
 */
public class DateList2 {


    static List<DefData> initList(LocalDate startDate, LocalDate endDate,
                                  List<DefCountData> defAddDate, List<DefCountData> defResolvedDate) {
        List<DefData> resultList = new ArrayList<>();

        while (!startDate.isAfter(endDate)) {
            resultList.add(new DefData(startDate));
            startDate = startDate.plusDays(1);
        }

        Map<LocalDate, DefCountData> defAddDateMap = new HashMap<>();
        for (DefCountData data : defAddDate) {
            defAddDateMap.put(data.date, data);
        }

//        Map<LocalDate, DefCountData> defAddDateMap = defAddDate.stream().
//                collect(Collectors.toMap(DefCountData::getDate, Function.identity()));

        Map<LocalDate, DefCountData> defResolvedDateMap = new HashMap<>();
        for (DefCountData data : defResolvedDate) {
            defResolvedDateMap.put(data.date, data);
        }

//        Map<LocalDate, DefCountData> defResolvedDateMap = defResolvedDate.stream().
//                collect(Collectors.toMap(DefCountData::getDate, Function.identity()));

        for (DefData data : resultList) {
            DefCountData defAddCountData = defAddDateMap.get(data.date);
            if (defAddCountData != null) data.addCount = defAddCountData.count;
            DefCountData defResolvedCountData = defResolvedDateMap.get(data.date);
            if (defResolvedCountData != null) data.resolvedCount = defResolvedCountData.count;
        }
        return resultList;
    }

    public static void main(String[] args) {
        LocalDate startDate = LocalDate.parse("2023-09-15", DateTimeFormatter.ISO_LOCAL_DATE);
        LocalDate endDate = LocalDate.parse("2023-10-03", DateTimeFormatter.ISO_LOCAL_DATE);

        List<DefCountData> defAddDate = new ArrayList<>();
        defAddDate.add(new DefCountData(LocalDate.parse("2023-09-19", DateTimeFormatter.ISO_LOCAL_DATE), 1L));
        defAddDate.add(new DefCountData(LocalDate.parse("2023-09-23", DateTimeFormatter.ISO_LOCAL_DATE), 2L));
        defAddDate.add(new DefCountData(LocalDate.parse("2023-10-01", DateTimeFormatter.ISO_LOCAL_DATE), 3L));

        List<DefCountData> defResolvedDate = new ArrayList<>();
        defResolvedDate.add(new DefCountData(LocalDate.parse("2023-09-23", DateTimeFormatter.ISO_LOCAL_DATE), 3L));
        defResolvedDate.add(new DefCountData(LocalDate.parse("2023-09-25", DateTimeFormatter.ISO_LOCAL_DATE), 2L));
        defResolvedDate.add(new DefCountData(LocalDate.parse("2023-10-01", DateTimeFormatter.ISO_LOCAL_DATE), 5L));

        long start = System.nanoTime();
        List<DefData> defData = initList(startDate, endDate, defAddDate, defResolvedDate);
        System.out.println("填充数据耗时：" + (System.nanoTime() - start)/1000 + " μs");
        for (DefData defDatum : defData) {
            System.out.println(defDatum);
        }
    }

    @Data
    static class DefData {
        LocalDate date;
        Long addCount = 0L;
        Long resolvedCount = 0L;

        public DefData(LocalDate date) {
            this.date = date;
        }

        public DefData(LocalDate date, Long addCount, Long resolvedCount) {
            this.date = date;
            this.addCount = addCount;
            this.resolvedCount = resolvedCount;
        }
    }

    @Data
    static class DefCountData{
        LocalDate date;
        Long count = 0L;

        public DefCountData(LocalDate date, Long count) {
            this.date = date;
            this.count = count;
        }
    }


}
//怎样将targetDate根据日期对应填充到dateList中